using System;
using System.Collections.Generic;
using System.Linq;
using log4net;
using SpeakOut.Data.Interfaces;
using SpeakOut.Data.NHibernate.Daos;
using SpeakOut.Lib;
using System.Net.Mail;
using System.Text;
using SpeakOut.Model;
using StructureMap;

namespace SpeakOut.Service
{
    public class FrontEndService : IFrontEndService
    {
        private static readonly ILog _log = LogManager.GetLogger(typeof(FrontEndService));

        private readonly IPostDao _postDao;
        private readonly ICategoryDao _categoryDao;

        public FrontEndService() : this(ObjectFactory.GetInstance<IPostDao>(), ObjectFactory.GetInstance<ICategoryDao>())
        {
        }

        public FrontEndService(IPostDao postDao, ICategoryDao categoryDao)
        {
            _postDao = postDao;
            _categoryDao = categoryDao;
        }

        public IList<Post> GetPosts()
        {
            return _postDao.FindAllVisible();
        }

        public IList<Post> GetPostsByMonth(int year, int month)
        {
            return _postDao.FindByMonth(year,month).ToList();
        }

        public IList<Post> GetPostsByCategory(string categoryName)
        {
            Category category = _categoryDao.FindByName(categoryName);
            return category == null ? new List<Post>() : _postDao.FindByCategory(category.CategoryId);
        }

        public IList<Post> GetPostsByTag(string tag)
        {
            return _postDao.FindByTag(tag);
        }

        public IList<Post> SearchPosts(string value)
        {
            return (from p in _postDao.FindAllVisible()
                    where p.Title.Contains(value) ||
                          p.Body.Contains(value)
                    select p).ToList();
        }

        public Post GetPost(string permalink, DateTime publishDate)
        {
            return _postDao.FindByPermalink(permalink, publishDate);
        }

        public Post GetPost(Guid guid)
        {
            return _postDao.FindById(guid);
        }

        public IList<Post> GetRecentPosts(int quantity)
        {
            return _postDao.FindRecentVisible(quantity);
        }

        public IList<Category> GetCategories()
        {
            return _categoryDao.FindAll();
        }

        public IList<Comment> GetComments(Guid postId)
        {
            return _postDao.FindById(postId).Comments.ToList();
        }

        public IList<Category> GetCategories(Guid postId)
        {
            return _postDao.FindById(postId).Categories.ToList();
        }

        public void SaveComment(Comment comment)
        {
            ICommentDao commentDao = new CommentDao();
            commentDao.MakePersistent(comment);
            notifyCommentAdded(comment);
        }

        private static void notifyCommentAdded(Comment comment)
        {
            if (!BlogConfig.ShouldNotifyNewComment) return;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("From: {0}({1})",comment.Author, comment.Email);
            sb.AppendLine();
            sb.AppendFormat("{0} ({1})", comment.Post.Title, comment.Post.PostId);
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendLine(comment.Body);
            
            SmtpClient client = new SmtpClient();
            client.Send(BlogConfig.BlogEmail, BlogConfig.ContactEmail,
                        "New Comment Posted", sb.ToString());
        }
    }
}